package com.anolesoft.twelvePlan.controller;

import cn.hutool.core.bean.BeanUtil;
import com.anolesoft.common.core.constant.HttpStatus;
import com.anolesoft.common.core.domain.LoginUser;
import com.anolesoft.common.core.web.domain.AjaxResult;
import com.anolesoft.twelvePlan.dto.MesPlanGradeDTO;
import com.anolesoft.twelvePlan.service.MesPlanGradeService;
import com.anolesoft.twelvePlan.vo.MesPlanGradeVO;
import com.anolesoft.yr.demo.PageResult;
import com.anolesoft.yr.util.Constants;
import com.anolesoft.yr.util.PageData;
import com.anolesoft.yr.util.SecurityUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author zyq
 * @date 2022/5/28
 */
@RestController
@RequestMapping(value="/mesPlanGrade")
@Slf4j
public class MesPlanGradeController {
    @Autowired
    private MesPlanGradeService mesPlanGradeService;

    @GetMapping(value = "/query")
    public AjaxResult queryPlanGrade(MesPlanGradeVO mesPlanGradeVO){
        log.debug("mesPlanGradeVO: {}", mesPlanGradeVO);
        LoginUser loginUser = SecurityUtils.getLoginUser();
        mesPlanGradeVO.setFactoryId(loginUser.getFacCode());
        log.debug("{}");
        Integer pageSize = mesPlanGradeVO.getPageSize();
        Integer pageNo = mesPlanGradeVO.getPageNo();
        MesPlanGradeDTO mesPlanGradeDTO = BeanUtil.copyProperties(mesPlanGradeVO, MesPlanGradeDTO.class);
        log.debug("{}",mesPlanGradeDTO);
        PageData<List<MesPlanGradeDTO>> listPageData = this.mesPlanGradeService.queryPlanGrade(mesPlanGradeDTO, pageNo, pageSize);
        List<MesPlanGradeVO> mesPlanGradeVOS = listPageData.getItems().stream()
                .map(t -> BeanUtil.copyProperties(t, MesPlanGradeVO.class))
                .collect(Collectors.toList());
        PageResult pageResult = new PageResult(mesPlanGradeVOS, pageSize, pageNo, listPageData.getTotal());
        return new AjaxResult(HttpStatus.SUCCESS, Constants.QUERY_SUCCESS_MESSAGE, pageResult);
    }

    @PostMapping(value = "/insert")
    public AjaxResult insertPlanGrade(@RequestBody MesPlanGradeVO mesPlanGradeVO){
        log.debug("mesWXInfoVO: {}", mesPlanGradeVO);
        LoginUser loginUser = SecurityUtils.getLoginUser();
        mesPlanGradeVO.setFactoryId(loginUser.getFacCode());
        mesPlanGradeVO.setCreateId(loginUser.getEmpUid());
        MesPlanGradeDTO mesPlanGradeDTO = BeanUtil.copyProperties(mesPlanGradeVO, MesPlanGradeDTO.class);
        log.debug("{}",mesPlanGradeDTO);
        String result = this.mesPlanGradeService.insertPlanGrade(mesPlanGradeDTO);
        if (result == "success") {
            return AjaxResult.success("新增成功！");
        } else if (result == "repeat"){
            return AjaxResult.success("该条信息已经存在！");
        } else {
            return AjaxResult.success("新增失败，请重试");
        }
    }

    @PostMapping(value = "/delete")
    public AjaxResult deletePlanGrade(@RequestBody MesPlanGradeVO mesPlanGradeVO){
        log.debug("mesWXInfoVO: {}", mesPlanGradeVO);
        String objuid = mesPlanGradeVO.getObjuid();
        log.debug("{}",objuid);
        this.mesPlanGradeService.deletePlanGrade(objuid);
        return AjaxResult.success();
    }
}